import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class YaSearchEngine implements SearchEngine {
    static final String titleUrlCssClass = ".b-serp-item__title-link";

    protected List<String> getLinks(String query) throws IOException {
        ArrayList<String> result = new ArrayList<String>();

        query = URLEncoder.encode(query, "UTF-8");

        Document doc = Jsoup.parse(new URL("http", "yandex.ru", "/yandsearch?text=" + query + "&lr=2"), 10000);
        Elements links = doc.select(titleUrlCssClass);
        for(Element element : links){
            String link = element.attr("href");
            if (link != null) {
                result.add(link);
            }
        }

        return result;
    }

    int goodQueries = 0;
    int badQueries = 0;

    protected void getDocument(String uri, ArrayList<WebDocument> docs) throws Exception {
        try{
            Document html = Jsoup.parse(new URL(uri), 3000);
            WebDocument doc = WebDocument.createFromHtml(html.html());
            docs.add(doc);
            goodQueries++;
        }
        catch(Exception e)
        {
            badQueries++;
            if (badQueries > 100) throw new Exception();
        }
    }

    public List<WebDocument> search(String query) throws Exception {
        ArrayList<WebDocument> result = new ArrayList<WebDocument>();

        for(String uri : getLinks(query))
        {
            getDocument(uri, result);
        }

        return result;
    }
}
